package com.android.boncen.commonFrame.user;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.android.boncen.commonFrame.R;
import com.android.boncen.commonFrame.bean.GlucoseHistoryBean;
import com.android.boncen.commonFrame.bean.UserBean;
import com.android.boncen.commonFrame.tools.DesityUtil;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.datatype.BmobDate;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;

/**
 * 功能描述:那里面显示最近血糖情况（过高过低正常的次数）
 *
 * @date 2017/2/9
 */

public class SugarSizeFragment extends Fragment {
    private TextView high_counter;
    private TextView normal_counter;
    private TextView low_counter;
    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        getWeek();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_user_info_view,container,false);
        high_counter = (TextView) view.findViewById(R.id.high_counter);
        normal_counter = (TextView) view.findViewById(R.id.normal_counter);
        low_counter = (TextView) view.findViewById(R.id.low_counter);
        return view;
    }

    /***
     * 一周记录
     */
    private void getWeek() {
        BmobQuery<GlucoseHistoryBean> query = new BmobQuery<GlucoseHistoryBean>();
        List<BmobQuery<GlucoseHistoryBean>> and = new ArrayList<BmobQuery<GlucoseHistoryBean>>();
        //大于00：00：00
        BmobQuery<GlucoseHistoryBean> q1 = new BmobQuery<GlucoseHistoryBean>();

        Calendar calendar = Calendar.getInstance();


        calendar.setTime(new java.util.Date());
        calendar.set(Calendar.DAY_OF_WEEK,Calendar.SUNDAY);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);

        q1.addWhereGreaterThanOrEqualTo("createdAt", new BmobDate(calendar.getTime()));
        and.add(q1);
        //小于23：59：59
        BmobQuery<GlucoseHistoryBean> q2 = new BmobQuery<GlucoseHistoryBean>();
        calendar.set(Calendar.DAY_OF_WEEK,Calendar.SATURDAY);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        q2.addWhereLessThanOrEqualTo("createdAt", new BmobDate(calendar.getTime()));
        and.add(q2);

        //添加复合与查询
        query.and(and);
        UserBean user = BmobUser.getCurrentUser(UserBean.class);

        query.addWhereEqualTo("userId", user.getObjectId());
        query.addWhereEqualTo("typeBlood", 0);
        query.findObjects(new FindListener<GlucoseHistoryBean>() {
            @Override
            public void done(List<GlucoseHistoryBean> list, BmobException e) {
                if (list == null || list.isEmpty()) {
                    return;
                }
                setDataView(list);
            }
        });
    }

    private void setDataView(List<GlucoseHistoryBean> list) {


        int red = 0;
        int yellow = 0;
        int green = 0;
        for (GlucoseHistoryBean bean : list) {
            float bloodNum = bean.getBloodSugar();
            if (bloodNum > 10.0) {

                red++;

            } else if (bloodNum < 4.0) {

                yellow++;
            } else {

                green++;
            }
        }

        high_counter.setText(Html.fromHtml("<font color=\"#FF3030\">" + red + "次</font>"));
        normal_counter.setText(Html.fromHtml("<font color=\"#228B22\">" + green + "次</font>"));
        low_counter.setText(Html.fromHtml("<font color=\"#EEAD0E\">" + yellow + "次</font>"));

    }
}
